<script setup name="content">
import { reactive, ref } from "vue"
import { toast } from "@/composables/util"
import { readGoods, updataGoods } from "@/api/goods"
import FormDialog from "@/components/FormDialog.vue"
import Editor from "@/components/Editor.vue"

// 表单数据
const form = reactive({
  content: ""
})
// 加载
const loading = ref(false)
// 当前商品id
const goodsId = ref(0)
const dialogRef = ref(null)
const emit = defineEmits(["reload"])

// 显示对话框
const open = async row => {
  goodsId.value = row.id
  loading.value = true
  dialogRef.value.open()
  const res = await readGoods(goodsId.value)
  form.content = res.content
  loading.value = false
}
// 提交
const submit = async () => {
  try {
    dialogRef.value.showLoading()
    await updataGoods(goodsId.value, form)
    toast("设置商品详情成功！")
    emit("reload")
    dialogRef.value.close()
  } finally {
    dialogRef.value.hideLoading()
  }
}
// 向外导出方法
defineExpose({ open })
</script>

<template>
  <FormDialog ref="dialogRef" title="设置商品详情" top="5vh" :width="80" @submit="submit" destroy-on-close>
    <el-form :model="form" v-loading="loading">
      <el-form-item>
        <Editor v-model="form.content" />
      </el-form-item>
    </el-form>
  </FormDialog>
</template>

<style lang="scss" scoped>
:deep(.el-form-item__content) {
  justify-content: center;
}
</style>
